<?php


namespace app\common;

use Elasticsearch\ClientBuilder;

class ElasticsearchClient
{

    public function index()
    {


        // 创建 Elasticsearch 客户端
        $client = ClientBuilder::create()->setHosts(['http://localhost:9200'])->build();

        // 示例数据
        $data = [
            'id' => 1,
            'content' => '这是一个测试文档11'
        ];

        // 插入数据
        try {
            $response = $client->index([
                'index' => 'my_index',
                'body' => $data
            ]);

            // 检查响应
            if ($response['result'] === 'created' || $response['result'] === 'updated') {
                return "数据插入成功";
            } else {
                return "数据插入失败";
            }
        } catch (\Exception $e) {
            return "发生错误：" . $e->getMessage();
        }


    }


}